<template>
  <div class="dev">
    <!-- 搜索框 -->
    <div class="search">
      <el-input placeholder="请输入手续费名称" clearable prefix-icon="el-icon-search" v-model="chargeName">
      </el-input>
      <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      <el-button type="success" icon="el-icon-plus" @click="DialogVisible = true">添加</el-button>
    </div>
    <!-- 表格数据 -->
    <div class="table">
      <el-table :data="tableData" ref="isExpand" v-loading="loading" border height="calc(100%-10rem)"
        highlight-current-row stripe style="width: 100%">
        >
        <template slot="empty">
          <el-empty description="暂无数据"></el-empty>
        </template>
        <el-table-column align="center" prop="serviceChargeName" label="名称">
        </el-table-column>
        <el-table-column align="center" prop="serviceCharge" label="手续费">
        </el-table-column>
        <el-table-column align="center" prop="tenantName" label="租户">
        </el-table-column>
        <el-table-column align="center" prop="isDefault" label="默认手续费">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.isDefault == true">是</el-tag>
            <el-tag v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="createTime" label="创建时间">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="openEditFn(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="delFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination v-show="tableData.length !== 0" background @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[5, 10, 15, 20, 25, 30]"
        :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 添加 -->
    <div class="add">
      <el-dialog :close-on-click-modal="false" title="新增" :visible.sync="DialogVisible" width="600px" @close="close">
        <el-form :model="addForm" ref="addForm" label-width="100px" class="demo-ruleForm" :rules="rules">
          <el-form-item prop="tenantId" label="租户">
            <el-select v-model="addForm.tenantId" filterable clearable placeholder="请选择租户">
              <el-option v-for="item in tenantLists" :key="item.tenantId" :label="item.tenantName" :value="item.tenantId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="手续费名称" prop="serviceChargeName">
            <el-input v-model="addForm.serviceChargeName" clearable placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item label="手续费" prop="serviceCharge">
            <el-input v-model="addForm.serviceCharge" clearable placeholder="请输入"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="DialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addFn">确 认</el-button>
        </span>
      </el-dialog>
    </div>
    <!-- 编辑 -->
    <div class="edit">
      <el-dialog :close-on-click-modal="false" title="编辑" :visible.sync="editDialogVisible" width="600px">
        <el-form :model="editForm" ref="editForm" label-width="100px" class="demo-ruleForm" :rules="rules">
          <el-form-item label="手续费名称" prop="serviceChargeName">
            <el-input v-model="editForm.serviceChargeName" clearable placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item label="手续费" prop="serviceCharge">
            <el-input v-model="editForm.serviceCharge" clearable placeholder="请输入"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editFn">确 认</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
  
<script>
import {
  getChargeApi,
  addChargeApi,
  editChargeApi,
  delChargeApi,
  getTenantApi,
} from "../../../../api/system/charge";
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: null,
      DialogVisible: false,
      editDialogVisible: false,
      chargeName: "",
      addForm: {
        serviceCharge: "",
        serviceChargeName: "",
        tenantId: "",
      },
      editForm: {
        serviceCharge: "",
        serviceChargeName: "",
      },
      editId: null,
      tenantLists: [],
      rules: {
        tenantId: {
          required: true,
          message: "请选择租户",
          trigger: ["blur", "change"],
        },
        serviceChargeName: {
          required: true,
          message: "手续费名称不能为空",
          trigger: ["blur", "change"],
        },
        serviceCharge: {
          required: true,
          message: "手续费不能为空",
          trigger: ["blur", "change"],
        },
      },
    };
  },
  created() {
    this.getTenant();
    this.pageSize = Number(sessionStorage.getItem("pageSize")) || 10;
    this.pageNum = Number(sessionStorage.getItem("pageNum")) || 1;
    this.getCharge(this.pageNum, this.pageSize);
  },
  destroyed() {
    sessionStorage.removeItem("pageNum");
    sessionStorage.removeItem("pageSize");
  },
  methods: {
    // 获取租户
    getTenant() {
      getTenantApi().then((res) => {
        if (res.code == 200) {
          this.tenantLists = res.data;
        }
      });
    },
    // 搜索
    search() {
      this.pageNum = 1
      this.getCharge(this.pageNum, this.pageSize, this.chargeName);
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
    // 获取手续费列表
    getCharge(pageNum, pageSize, chargeName) {
      this.loading = true;
      getChargeApi(pageNum, pageSize, chargeName).then((res) => {
        this.total = res.data.total;
        if (res.data.rows.length == 0) {
          this.pageNum = this.pageNum - 1;
          if (this.pageNum > 0) {
            this.search();
          } else {
            this.tableData = res.data.rows;
            this.loading = false;
          }
        } else {
          this.loading = false;
          this.tableData = res.data.rows;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.pageNum = 1
      this.getCharge(this.pageNum, val, this.chargeName);
      sessionStorage.setItem("pageSize", val);
      this.pageSize = val;
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
    handleCurrentChange(val) {
      this.getCharge(val, this.pageSize, this.chargeName);
      sessionStorage.setItem("pageNum", val);
      this.pageNum = val;
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
    // 添加
    addFn() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          addChargeApi(this.addForm)
            .then((res) => {
              this.DialogVisible = false;
              this.search();
              this.$message.success("添加成功");
            })
            .catch((err) => {
              this.$message.error("添加失败");
            });
        } else {
          this.$message.error("请检查填写并重试");
        }
      });
    },
    // 关闭添加框
    close() {
      this.addForm = {
        serviceCharge: "",
        serviceChargeName: "",
      };
    },
    // 打开编辑框
    openEditFn(e) {
      console.log(e);
      this.editId = e.serviceChargeId;
      this.editDialogVisible = true;
      this.editForm = e;
    },
    // 编辑
    editFn() {
      editChargeApi(this.editId, this.editForm)
        .then((res) => {
          this.$message.success("操作成功");
          this.editDialogVisible = false;
          this.search();
        })
        .catch((err) => {
          this.$message.error("操作失败");
        });
    },
    // 删除
    delFn(e) {
      this.$confirm("确认删除此费率？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delChargeApi(e.serviceChargeId)
            .then((res) => {
              this.$message.success("删除成功");
              this.search();
            })
            .catch((err) => {
              this.$message.error("删除失败");
            });
        })
        .catch(() => {
          this.$message.success("已取消删除");
        });
    },
  },
};
</script>
  
<style lang="scss" scoped>
.dev {
  padding-top: 15px;
  padding: 0 15px;
  height: calc(100vh - 100px);

  .search {
    margin-bottom: 15px;

    .el-input {
      width: 200px;
    }

    .el-button {
      //   width: 120px;
      margin-left: 15px;
    }
  }

  .table {
    height: 100%;

    .el-table {
      height: 70%;
    }

    .el-pagination {
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>